package su.kozhevnikov.nookbrary.items;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import android.database.Cursor;

import su.kozhevnikov.nookbrary.Util;
import su.kozhevnikov.nookbrary.interfaces.IFileItem;


public class FileItem extends BaseItem
{
	protected File _file=null;
    protected Map<String, String> _data = new HashMap<String, String>();	
    protected static final String SEPARATOR =", ";
	protected String _path = null;
	protected String _mimetype=null;
    Date _date=null;
    
	
	public Date getDate()
	{
		return _date;
	}
    
    public void setDate()
    {
    	_date=new Date();
    }
	
	public String getMimeType()
	{
		return _mimetype;
	}    
    
	public String getPath()
	{
		return _path;
	}	
	
	public FileItem(String path)
	{
		super();
		_path= path;
	}

	public FileItem(Cursor cursor)
	{
		super();
		this._author = cursor.getString(2);
		this._title = cursor.getString(1);
		this._seriesname = cursor.getString(3);
		this._seriesindex = cursor.getInt(4);
		_path= cursor.getString(5);
		this._authorcount = cursor.getInt(6);
		this._author_extra = cursor.getString(7);
	}

	public FileItem()
	{
		super();
		_path= null;
		_file=null;
	}	

	
	public void FromPath(String path,Date date)
	{
		_path = path;
		_date = date;
		Prepare();
	}
	
	
	public Boolean compareTo(IFileItem item)
	{
		if (item==null) return false;
		return getPath().compareTo(item.getPath())==0;
		
	}
	
	public Boolean Prepare()
	{
		if (_file==null)
		{
			if ( Util.IsNullOrEmpty(_path) ) return false;
			_file = new File(_path);
		}
		
		_size = _file.length();
		_path = _file.getAbsolutePath();
		
		return (_file!=null);
		
	}

	static SimpleDateFormat _dateformat = new SimpleDateFormat("dd.MM.yy HH:mm"); 
	
	public String getSizeStr()
	{
		return _date==null ? super.getSizeStr() : _dateformat.format(_date);
	}
	
	protected void setAuthorToCanonicalView() {
		if (this._author.trim().length() == 0) {
			this._authorcount = 0;
			return;
		}
		this.allAuthors = new ArrayList<String>(); 
		if (this._author.contains(",")) {
			for (String oneAuthor: this._author.split("\\s*,\\s*")) {
				allAuthors.add(oneAuthor.trim());
			}
		}
		else {
			allAuthors.add(this._author.trim());
		}
		//Log.d(Bibliotheca.LOGTAG, "Count of author = " + allAuthors.size());
		String result = "";
		for (int i = 0; i < allAuthors.size(); i++) {
			String author = allAuthors.get(i);
			if (author.contains(" ")) {
				String lastName = author.substring(author.lastIndexOf(" "));
				String firstName = author.substring(0, author.lastIndexOf(" "));
				author = lastName.trim() + " " + firstName.trim();
				allAuthors.set(i, author);
			}
			if (result.length() > 0)
				result += ", ";
			result += author;
			this._authorcount++;
		}
		this._author = result;
	}
}
